<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/3/14
 * Time: 14:28
 */

namespace app\api\controller;

use app\admin\controller\BaseAdminController;
use app\api\model\Cart as CartModel;
use app\api\model\GoodsRule;
use app\api\validate\CartValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Response;

class Cart extends BaseAdminController
{
    /**
     * 添加购物车
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function add(): Response
    {
        $param = request()->param();
        validate(CartValidate::class)->scene('add')->check($param);
        //校验商品是否存在
        if (!$rule = GoodsRule::where('id', $param['goods_rule_id'])->find()) return $this->error('商品不存在');
        //校验商品是否上架
        if ($rule->status == 2) return $this->error('商品已下架');
        $model = CartModel::where('user_id', $this->user_id)->where('goods_rule_id', $param['goods_rule_id'])->find();
        //todo 购物车要不要存放单价,如果价格变更.....
        if ($model) {
            if (request()->param('is_input') == 1) {
                if ($model->quantity + $param['quantity'] > 0) {
                    $model->inc('quantity', $param['quantity'])->inc('amount', $model->money * $param['quantity'])->update();
                }
                //当数量为0时删除
                if ($model->quantity + $param['quantity'] <= 0) {
                    $model->inc('quantity', -$model->quantity)->update();
                    $model->inc('amount', $model->money * (-$model->quantity))->update();
                    $model->delete();
                }
            }
            if (request()->param('is_input') == 2) {
                $model->quantity = $param['quantity'];
                $model->amount   = $model->money * $param['quantity'];
                $model->save();
            }
        } else {
            $param['goods_id'] = $rule->goods_id;
            $param['money']    = $rule->money;
            $param['img']      = $rule->img;
            $param['user_id']  = $this->userInfo->id;
            $param['amount']   = $rule->money * $param['quantity'];
            CartModel::create($param);
        }
        return $this->success('操作成功');
    }

    /**
     * 列表
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list(): Response
    {
        $param = request()->param();
        validate(CartValidate::class)->scene('list')->check($param);
        $sql   = CartModel::where('user_id', $this->user_id);
        $count = $sql->count();
        $sql   = $sql->page($param['page'], $param['limit']);
        $list  = $sql->order('create_time desc')->select()->each(function ($item) {
            $item->goods_rule = $item->goodsRule()->field('money,spec_name')->find();
            $item->goods      = $item->goods()->field('id,name,img')->find();
        });
        return $this->success('操作成功', compact('list', 'count'));
    }

}